<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\bill;

use app\common\dao\BaseDao;
use app\common\model\bill\BillCourseModel;
use app\common\repositories\bill\BillBasicRepository;

class BillCourseDao extends BaseDao
{

    /**
     * @return BillCourseModel
     */
    protected function getModel(): string
    {
        return BillCourseModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['bill_id']) && $where['bill_id'] !== '', function ($query) use ($where) {
                $query->where('bill_id', (int)$where['bill_id']);
            })
            ->when(isset($where['pay_type']) && $where['pay_type'] !== '', function ($query) use ($where) {
                $query->where('pay_type', (int)$where['pay_type']);
            })
            ->when(isset($where['pay_method']) && $where['pay_method'] !== '', function ($query) use ($where) {
                $query->where('pay_method', (int)$where['pay_method']);
            })
            ->when(isset($where['check_type']) && $where['check_type'] !== '', function ($query) use ($where) {
                $query->where('check_type', (int)$where['check_type']);
            })
            ->when(isset($where['affirm_type']) && $where['affirm_type'] !== '', function ($query) use ($where) {
                $query->where('affirm_type', (int)$where['affirm_type']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $billBasicRepository = app()->make(BillBasicRepository::class);
                $billIds = $billBasicRepository->getSearch([])->whereLike('number', '%'.$where['keywords'].'%')->column('id');
                $query->whereIn('bill_id', $billIds);
            })
            ->when(isset($where['pay_time']) && $where['pay_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['pay_time'], 'pay_time');
            })
            ->when(isset($where['check_time']) && $where['check_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['check_time'], 'check_time');
            })
            ->when(isset($where['affirm_time']) && $where['affirm_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['affirm_time'], 'affirm_time');
            });
    }

    public function deleteByBillId(int $id)
    {
        return $this->getSearch([
            'bill_id' => $id
        ])->delete();
    }
}
